package com.mike.bdb.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.viewers.TableViewer;

import com.mike.bdb.ui.provider.table.PageInfo;
import com.mike.bdb.ui.provider.tree.TreeNode;


public class DataCurrent {
	
	public static final String VERSION = "1.0.4";
	private static String	VERSION_FILE = "version";
	
	private static DataCurrent instance = new DataCurrent();

	/**
	 * 所有打开的tableViewer
	 * 映射方式String key = 
	 * DataCurrent.getInstance().curDataBasePath + [":N:"] + ":L:" + bdb... + store + className;
	 * */
	public static final Map<String, TableViewer> openTableViewers = new HashMap<String, TableViewer>();
	
	private DataCurrent() {
		//Don't allow new it outside.
	}
	
	public static DataCurrent getInstance() {
		return instance;
	}
	
	/** 打开的数据库 */
	public volatile Set<TreeNode> openDatabase = new HashSet<TreeNode>();
	
	/** 导入文件 */
	public volatile File curImportFile = null;
	
	/** version 2 采用完整类名判断 */
	public volatile String curStoreClazz = null;
	
	/** 当前数据库 */
	public volatile String curDataBasePath = null;
	
	private volatile PageInfo pageInfo = new PageInfo(0, 0, 50);
	
	private volatile PageInfo secondaryPageInfo = new PageInfo(0, 0, 20);
	
	private volatile PageInfo queryPageInfo = new PageInfo(0, 0, 30);
	
	private volatile PageInfo queryPKPageInfo = new PageInfo(0, 0, 10);

	/** 主表分页数据 */
	public PageInfo getPageInfo() {
		return pageInfo;
	}
	
	/** 次表分页数据 */
	public PageInfo getSecondaryPageInfo() {
		return secondaryPageInfo;
	}
	
	/** 条件查询表分页数据 */
	public PageInfo getQueryPageInfo() {
		return queryPageInfo;
	}
	
	/** 主键查询表分页数据 */
	public PageInfo getQueryPKPageInfo() {
		return queryPKPageInfo;
	}
	
	public static String getVersionHistory() {
		StringBuffer sb = new StringBuffer();
		InputStream verStream = DataCurrent.class.getResourceAsStream(VERSION_FILE);
		BufferedReader br = new BufferedReader(new InputStreamReader(verStream, Charset.forName("UTF-8")));
		String temp = "";
		try {
			try {
				while (null != (temp = br.readLine())) {
					sb.append(" " + temp);
					sb.append("\n");
				}
			} finally {
				br.close();
			}
		} catch (IOException e) {
			//ignore
		}
		return sb.toString();
	}
	
	
}
